var zTreeObj;
var cNode;
var setting = {
	check : {
		enable : false
	},
	async : {
		enable : true,
		url : "newscatalog/news-catalog!listbyparent",
		autoParam : [ "id=bean.id" ],
		otherParam : {}
	},
	callback : {
		onClick : zTreeOnClick,
		onExpand: zTreeOnExpand
	}
};
var root = {
	name : "所有新闻分类",
	open : true,
	nocheck : false,
	id : "root",
	isParent : true
};
var schild = $("#firstId");
$(function() {
	
	var zNodes = [ root ];
	zTreeObj = $.fn.zTree.init($("#resTree"), setting, zNodes);
	var nodes = zTreeObj.getNodes();
	zTreeObj.expandNode(nodes[0], null, false, false, true);
	
	$("#saveBtn").bind("click", function() {
		var cid = $("#bsid").val();
		if (cid == null || cid == "") {
			alert("请先选择一个新闻分类!");
			return;
		}

		submitAll();

	});

});
function zTreeOnClick(event, treeId, treeNode) {
	
	if (treeNode.isParent && treeNode.id != 'root') {
		$("#bsid").val(treeNode.id);
		$("#bname").val(treeNode.name);
		$("#cname").text(treeNode.name);

		$.getJSON("newscatalog/news-catalog!catalog",
				"bean.id=" + treeNode.id, fillSelectChild);

	} else {
		$("#cname").text("请选择一个目录!");
	}
}
function zTreeOnExpand(event,treeId,treeNode){
	
	$.getJSON("newscatalog/news-catalog!catalog",
			"bean.id=" + treeNode.id, fillSelectChild);
}
function fillSelectChild(data) {
	schild.empty();
	for ( var i = 0; i < data.length; i++) {
		var d = data[i];
		var opt = $("<li></li>");
		var rdo = $("<label><input type='radio' name='bean.defaultFirstSid' value='"+d.id+"'></input>"+d.name+"</label>");
		opt.append(rdo);
		schild.append(opt);
	}

}

function submitAll() {

	var data = $("#NewsCatalogForm").serialize();
	alert(data);
	var url = "newsconfig/news-config!create";
	doSubmit(url, data);
}